<?php

require_once 'ActionFramework/formcomponents/list.php';
require_once 'actions/_user_settings.cls.php';
require_once 'util.php';

class LanguageListField extends ListField {
	public function __construct($claimed_input) {
		parent::__construct($claimed_input, new ListDropDownField(null, LanguageCodeInput::$languages));
	}
}


/**
 * This is a widget that seems to be 1 forminput but actually consists of 2
 */
// TODO: Would be nice if we could use a NonClaimingFormInout
class _UnitLanguageLevelField extends FormComposite implements FormInput {
	
	public function __construct() {
		parent::__construct();
		$this->add('language', new ListDropDownField(null, LanguageCodeInput::$languages));
		$this->add('level', new ListDropDownField(null, LevelInput::$levels));
	}
	
	public function get_js_getter() {
		$language = $this->c['language']->get_js_getter();
		$level = $this->c['level']->get_js_getter();
		return "(function () { return [$language(), $level()] })";  
	}
	
	public function get_js_user_value_getter() {
		$language = $this->c['language']->get_js_user_value_getter();
		$level = $this->c['level']->get_js_user_value_getter();
		return "(function () { return $language() + ' - ' + $level() })";  
	}
	
	public function get_js_setter() {
		$language = $this->c['language']->get_js_setter();
		$level = $this->c['level']->get_js_setter();
		return "(function (value) { 
			if (value === '') {
				$language(''); 
				$level('');
			}
			else {
				$language(value[0]); 
				$level(value[1]);
			}
		})";  
	}
	
	public function set_initial_value($value) {
		throw new Exception("Not supported");
	}
	
	public function get_js_preprocessing() {
		return array();
	}

	public function get_js_postprocessing() {
		return array();
	}
	
	public function get_checks() {
		return null;
	}
	
	public function is_claiming() {
		return false;
	}
	
	public function get_claimed_input_name() {
		return null;
	}
	
	public function set_granted_input(Input $input) {
	}
	
	public function get_granted_input() {
		return null;
	}
	
}


class LanguageLevelListField extends ListField {
	
	public function __construct($claimed_input) {
		parent::__construct($claimed_input, new _UnitLanguageLevelField());	 	
	 	// For extra width
	 	$this->wrap_in_div('lang-formcomponent');
	}

}

